package com.wpf.system.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wpf.system.entity.vo.CascaderVO;
import com.wpf.system.common.utils.MakeTree;
import com.wpf.system.entity.Menu;
import com.wpf.system.service.MenuService;
import com.wpf.system.mapper.MenuMapper;
import com.wpf.system.service.RoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author ferna
* @description 针对表【menu(权限表（目录->菜单->权限）)】的数据库操作Service实现
* @createDate 2022-12-15 22:31:17
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService{

    @Resource
    RoleMenuService roleMenuService;

    public List<Menu> getUserMenuById(Long id) {
        return this.baseMapper.getUserMenuById(id);
    }

    @Override
    public List<Menu> getMenuList() {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Menu::getSort);
        List<Menu> menuList = this.baseMapper.selectList(queryWrapper);
        return MakeTree.makeMenuTree(menuList,0L);
    }

    @Override
    public List<CascaderVO> getParentMenuList() {
        String[] type = {"0","1"};
        List<String> typeList = Arrays.asList(type);
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Menu::getType,typeList).orderByAsc(Menu::getSort);
        List<Menu> menuList = this.baseMapper.selectList(queryWrapper);
        return MakeTree.makeMenuCascader(menuList,0L);
    }

    public int editMenuStatusById(Long id) {
        return this.baseMapper.editMenuStatusById(id);
    }

    @Override
    @Transactional
    public Boolean deleteMenuByd(Long id) {
        ArrayList<Long> ids = new ArrayList<>();
        //先把要删除的一级分类id放入到集合中
        ids.add(id);
        //递归的将一级分类下的id也加入到集合中
        this.getIds(ids,id);
        try {
            //批量删除集合中的id
            if(Convert.toBool(baseMapper.deleteBatchIds(ids))){
                //关联删除角色--权限表中的数据
                roleMenuService.deleteRoleMenuByMid(ids);
                return true;
            }else{
                return false;
            }
        }catch(RuntimeException e) {
            //回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public List<Menu> getMenuByRid(Long rid) {
        return this.baseMapper.getMenuByRid(rid);
    }

    public void getIds(ArrayList<Long> ids, Long oneId) {
        //查询二级分类的对象
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Menu::getPid,oneId);
        List<Menu> childrenList = baseMapper.selectList(queryWrapper);
        //遍历二级分类的对象，把二级分类的id加入到要删除的集合中
        for (Menu children : childrenList) {
            Long id = children.getId();
            ids.add(id);
            //把二级分类的每一个ID，查询它下面的子节点
            this.getIds(ids,id);
        }
    }

}




